//
//  FirstDesViewController.m
//  BreadTravel
//
//  Created by 张健 on 15/12/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "FirstDesViewController.h"
#import "LRJDesSecCityViewController.h"
#import "LRJDesFirCollectionViewCell.h"
#import "LRJDesFirModel.h"
#import "LRJDesFirCollectionReusableView.h"
#import "ZJDesSecMoreCityViewController.h"
#import "ZJScrollModel.h"
#import "WebViewControllerCJT.h"



//协议第四步:签订协议
@interface FirstDesViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, LRJDesFirCollectionReusableViewDelegate, SDCycleScrollViewDelegate>

@property (nonatomic, strong)UICollectionView *myCollectionView;

@property (nonatomic, strong)NSMutableArray *modelArray;

@property (nonatomic, strong)NSMutableArray *scrollArray;

@property(nonatomic,strong)UIPageControl *pageC;

@property(nonatomic,strong)SDCycleScrollView *cycleScrollView2;

@property(nonatomic,retain)UICollectionReusableView *headView;


@end

@implementation FirstDesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 15, 50, 20)];
    titleLabel.text = @"目的地";
    titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titleLabel;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置滚动方向(默认竖向)
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal
    //设置每个item的大小
    flowLayout.itemSize = CGSizeMake(WIDTH / 2 - 20, WIDTH / 2 - 20);
    //设置最小行间距
    flowLayout.minimumLineSpacing = 10;
    //设置最小列间距
//    flowLayout.minimumInteritemSpacing = 1;
    //设置item与边界的距离  上左下右
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 10, 10);
    
    //设置头部区域大小
    flowLayout.headerReferenceSize = CGSizeMake(WIDTH, 30);
    
    
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49 - 10) collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.backgroundColor = [UIColor colorWithRed:0.970 green:0.970 blue:0.920 alpha:1.000];
    
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    
    //CollectionView偏移量
    self.myCollectionView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
    self.modelArray = [NSMutableArray array];
    self.scrollArray = [NSMutableArray array];
    
    //请求数据
    [self getAFNetWorkingData];
    
    //注册cell
    [self.myCollectionView registerClass:[LRJDesFirCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    //注册头部区域
    [self.myCollectionView registerClass:[LRJDesFirCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
//    [self.myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header2"];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.modelArray.count;
}


#pragma mark -- 设置头部和底部区域内容
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    //创建头部区域view
#warning mark - 重写这个方法,解决重用问题
    LRJDesFirCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    headView.label.text = [[self.modelArray objectAtIndex:indexPath.section] title];
    //判断数据, 有的区没有全部, 要筛选
    BOOL more = [[self.modelArray objectAtIndex:indexPath.section] more];
    NSString *index = [[self.modelArray objectAtIndex:indexPath.section] index];
    headView.index = index;
    
    if (more == YES) {
        headView.button.frame = CGRectMake(WIDTH - 80, 10, 80, 30);
        [headView.button setTitle:@"全部 >" forState:UIControlStateNormal];
        [headView.button setTitleColor:[UIColor colorWithRed:0.573 green:0.529 blue:0.451 alpha:1.000] forState:UIControlStateNormal];
    }
    else
    {
        //将没有全部的区排出去
        headView.button.frame = CGRectMake(MAXFLOAT, 0, 0, 0);
    }
    
     //协议第五步:设置代理人
    headView.LRJReusableDelegate = self;
    return headView;

}
//协议第六步:实现协议方法
- (void)delegateFunction:(NSString *)string
{
    ZJDesSecMoreCityViewController *moreCityVC = [[ZJDesSecMoreCityViewController alloc] init];
    [self.navigationController pushViewController:moreCityVC animated:YES];
    moreCityVC.string = string;
}

#pragma mark -- 请求网络数据
- (void)getAFNetWorkingData
{
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"加载中";
    [AFNetworkTool getUrl:@"http://api.breadtrip.com/destination/v3/?last_modified_since=0" body:nil result:HXJSON headerFile:nil success:^(id result) {
        
        NSMutableDictionary *bigDic = result;
        NSMutableArray *elementsArr = [bigDic objectForKey:@"elements"];
        for (NSMutableDictionary *dic in elementsArr) {
            LRJDesFirModel *desFirModel = [LRJDesFirModel modelWithDictionary:dic];
            [self.modelArray addObject:desFirModel];
        }
        NSMutableArray *bannersArr = [bigDic objectForKey:@"banners"];
        NSMutableArray *tempArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in bannersArr) {
            ZJScrollModel *scrollModel = [ZJScrollModel modelWithDictionary:dic];
            [tempArr addObject:scrollModel.image_url];
            [self.scrollArray addObject:scrollModel];
        }
        
        SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, -200, WIDTH, 200) imageURLStringsGroup:tempArr]; // 模拟网络延时情景
        cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        cycleScrollView2.delegate = self;
        cycleScrollView2.dotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
        cycleScrollView2.placeholderImage = IMAGE;
        [self.myCollectionView addSubview: cycleScrollView2];
        
        //             --- 模拟加载延迟
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            cycleScrollView2.imageURLStringsGroup = tempArr;
        });
        
        
        [self.myCollectionView reloadData];
        [hub hide:YES];
    } failure:^(NSError *error) {
        
        NSLog(@"error = %@", error);
    }];
}
#pragma mark 点击轮播图
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
    WebViewControllerCJT *webVC =[[WebViewControllerCJT alloc]init];
    [self.navigationController pushViewController:webVC animated:YES];
    webVC.webURL = [[self.scrollArray objectAtIndex:index] html_url];
}

#pragma mark -- 必须实现的方法
//每个区的item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[[self.modelArray objectAtIndex:section]dataArray] count];
}
//每个item的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"Cell";
    LRJDesFirCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndentifier forIndexPath:indexPath];
    LRJDesFirDataModel *desFirDataModel = [[[self.modelArray objectAtIndex:indexPath.section] dataArray]objectAtIndex:indexPath.row];
    cell.dataModel = desFirDataModel;
    
    return cell;
}





#pragma mark -- 点击item触发的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LRJDesSecCityViewController *cityVC = [[LRJDesSecCityViewController alloc] init];
    [self.navigationController pushViewController:cityVC animated:YES];
    
    LRJDesFirDataModel *dataModel = [[[self.modelArray objectAtIndex:indexPath.section] dataArray]objectAtIndex:indexPath.row];
    cityVC.type = dataModel.type;
    cityVC.id = dataModel.id;
    cityVC.name_orig = dataModel.name_orig;
    cityVC.visited_count = dataModel.visited_count;
    cityVC.wish_to_go_count = dataModel.wish_to_go_count;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
